<?php
/**
 * Created by PhpStorm.
 * User: soft
 * Date: 2016/9/7
 * Time: 17:59
 * extends base_task_abstract implements base_interface_task
 */

//define('ROOT_DIR',realpath(dirname(__FILE__)));
define('ROOT_DIR', realpath(dirname(__FILE__).'/../../../../'));
date_default_timezone_set(
    defined('DEFAULT_TIMEZONE') ? ('Etc/GMT'.(DEFAULT_TIMEZONE>=0?(DEFAULT_TIMEZONE*-1):'+'.(DEFAULT_TIMEZONE*-1))):'UTC'
);

require(ROOT_DIR.'/app/base/kernel.php');
require(ROOT_DIR.'/config/config.php');
kernel::register_autoload();

class b2c_tasks_goodsearch {

    private $type = 2;
    private $cat_type = '9,10,11';

    /**
     * 同步虚拟分类属性的商品
     * @param
     */
    public function set_virtualcat_gid($m,$r){
        $sql = 'select virtual_cat_id,filter from sdb_b2c_goods_virtual_cat where type_id in('.$this->type.')';
        $info = $m->select($sql);
        foreach($info as $k=>&$v) {

            $filter = explode('&', $v['filter']);
            if (!empty($filter)) {
                foreach ($filter as $_k => $_v) {
                    $filter_props = explode('=', $_v);
                    if ($filter_props[0] == 'pricefrom') {
                        if ($filter_props[1] && $filter_props[1] != '_ANY_') {
                            $goods['price'][] = ' g.s_price >= ' . $filter_props[1];
                        }
                    }
                    if ($filter_props[0] == 'priceto') {
                        if ($filter_props[1] && $filter_props[1] != '_ANY_') {
                            $goods['price'][] = ' g.s_price <= ' . $filter_props[1];
                        }
                    }
                    if ($filter_props[0] == 'searchname') {
                        if ($filter_props[1] && $filter_props[1] != '_ANY_') {
                            $goods['like'][] = ' (g.name LIKE "%' . $filter_props[1] . '%" or bn like "' . $filter_props[1] . '%")';
                        }
                    }
                    if (substr($filter_props[0], 0, strpos($filter_props[0], '[')) == 'props') {
                        if ($filter_props[1] != '_ANY_') {
                            $goods_p = substr($filter_props[0], (strpos($filter_props[0], '[') + 1), (strpos($filter_props[0], ']') - 1 - strpos($filter_props[0], '[')));
                            $goods_pnum[] = $goods_p;
                            $goods['props'][] = ' (p.goods_p = ' . $goods_p . ' and p.props_value_id = ' . $filter_props[1] . ')';
                        }
                    }
                    if (substr($filter_props[0], 0, strpos($filter_props[0], '[')) == 'brand_id') {
                        if ($filter_props[1] != '_ANY_') {
                            $goods['brands'][] = $filter_props[1];
                        }
                    }
                    if (substr($filter_props[0], 0, strpos($filter_props[0], '[')) == 'cat_id') {
                        if ($filter_props[1] != '_ANY_') {
                            $goods['cats'][] = $filter_props[1];
                        }
                    }
                    unset($filter_props);
                }
                if ($goods['price']) {
                    $data[0] = implode(' and ', $goods['price']);
                }
                if ($goods['like']) {
                    $data[1] = implode(' or ', $goods['like']);
                }
                if ($goods['props']) {
                    $props_sql = "SELECT p.goods_id FROM  `sdb_b2c_goods_props_extend` p
                             WHERE " . implode(' or ', $goods['props']) .
                        " group by p.goods_id having count(1) >" . (count(array_unique($goods_pnum)) - 1);
                    //$data[1] = ' g.goods_id in(' . $props_sql . ') ';
                    //获取属性的goods_id
                    if($m->select($props_sql)){
                        foreach($m->select($props_sql) as $_psk=>$_psv) {
                            $props_sql_info_goods[] = $_psv['goods_id'];
                        }
                        $data[2] = ' g.goods_id in(' . implode(',',$props_sql_info_goods) . ') ';
                    }
                    unset($props_sql,$props_sql_info_goods);
                }
                if ($goods['brands']) {
                    $brand_sql = "SELECT brand_id FROM  `sdb_b2c_brand`
                                 WHERE brand_id in(" . implode(',', $goods['brands']) .
                        ") and  disabled = 'false'";
                    if($m->select($brand_sql)){
                        foreach($m->select($brand_sql) as $_bsk=>$_bsv) {
                            $brand_sql_info_goods[] = $_bsv['brand_id'];
                        }
                        $data[3] = ' g.brand_id in( ' . implode(',',$brand_sql_info_goods) . ') ';
                    }
                    unset($brand_sql,$brand_sql_info_goods);
                }
                if ($goods['cats']) {
                    foreach ($goods['cats'] as $_gcv) {
                        if (in_array($_gcv, explode(',', $this->cat_type))) {
                            //是父类
                            $parent_id[] = $_gcv;
                        }
                    }
                    if (!empty($parent_id)) {
                        $cats_sql = "SELECT cat_id FROM  `sdb_b2c_goods_cat`
                             WHERE parent_id in(" . implode(',', $parent_id) . ")";
                        if($m->select($cats_sql)){
                            foreach($m->select($cats_sql) as $_csk=>$_csv) {
                                $cats_sql_info_goods[] = $_csv['cat_id'];
                            }
                            $data[4] = '(g.cat_id in (' . implode(' , ', array_merge($goods['cats'],$cats_sql_info_goods)) . '))';
                        }
                    } else {
                        $data[4] = '(g.cat_id in (' . implode(' , ', $goods['cats']) . ')) ';
                    }
                    unset($cats_sql,$cats_sql_info_goods);
                }
                if (is_array($data) && count($data) > 0) {
                    $sql_end = implode(' and ', $data);
                }
                if ($sql_end) {
                    $sql = "SELECT g.goods_id FROM `sdb_b2c_goods` g
                             WHERE g.marketable = TRUE and g.goods_type='normal' and g.type_id = $this->type"
                        . ' and  ' . $sql_end . ' ' .
                        "GROUP BY g.`goods_id`
                             ";//print_r($sql);
                    print_r($v['virtual_cat_id']);echo '++++';echo '============/n/r';
                    $getall = $m->select($sql);
                    if ($getall) {
                        $key = 'virtual_goods_' . $v['virtual_cat_id'];
                        if($r->exists($key)){
                            $r->del($key);
                        }
                        foreach ($getall as $_gk => $_gv) {
                            //$value[] = $_gv['goods_id'];
                            $r->setAdd( $key, $_gv['goods_id'] );
                        }
                        unset($key);
                        //$value = implode(',', $value);
                        //$key = 'virtual_goods_' . $v['virtual_cat_id'];
                        //print_r($key);echo '<pre>';print_r($value);echo '<br/>+++++++++++++++';
                        //$r->store($key, $value);
                    }
                }
                unset($getall, $data, $parent_id, $goods_p, $goods_pnum, $goods, $getall, $sql_end, $sql);
            }
            //sleep(5);
            unset($filter, $goods);
        }

    }

    public function exec($params=null){
        //在这里写处理代码
        set_time_limit(0);
        $r = kernel::single('base_kvstore_redis2');
        $m =  kernel::database();
        $r->selects(1);

        $this->set_virtualcat_gid($m,$r);

        //$this->set_virtualcat_info($m,$r);
    }
}
$obj = new b2c_tasks_goodsearch();
$obj->exec();